﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Bll = FortuneArterial.Site.BusinessLogicLayer;

namespace FortuneArterial.Site.Manage.Controllers
{
    public class HomeController : Controller
    {
        public Bll.Database.Context context = new Bll.Database.Context();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Lock()
        {
            return View();
        }

        public ActionResult Sparks()
        {
            return PartialView();
        }

        public ActionResult Nav()
        {
            List<Models.NavUIModel> list = new List<Models.NavUIModel>();
            this.AddContent(list, "ST000001", 1);

            return PartialView(list.OrderBy(m => m.OrderNum));
        }

        private List<Models.NavUIModel> AddContent(List<Models.NavUIModel> list, string parent, int level)
        {
            var pageGroups = context.PageGroupInfoSet.Where(m => m.ParentID == parent).ToList();
            foreach (var pageGroup in pageGroups)
            {
                List<Models.NavUIModel> tempList = new List<Models.NavUIModel>();

                this.AddContent(tempList, pageGroup.ID, level + 1);

                if (tempList.Count > 0)
                {
                    list.Add(new Models.NavUIModel()
                    {
                        Icon = pageGroup.Icon,
                        Name = pageGroup.Name,
                        OrderNum = pageGroup.OrderNum,
                        Path = "#",
                        Child = tempList,
                        Level =  level
                    });
                }
            }

            var pages = context.PageInfoSet.Where(m => m.ParentID == parent);
            foreach (var page in pages)
            {
                list.Add(new Models.NavUIModel()
                {
                    Icon = page.Icon,
                    Name = page.Name,
                    Path = page.Path,
                    OrderNum = page.OrderNum,
                    Level = level
                });
            }

            return list.OrderBy(m => m.OrderNum).ToList();
        }

    }
}
